#include "hd.h"
#include "status.h"
#include <avr/eeprom.h>
#include "config.h"

//**************************************************
//
//                    cd
//
//**************************************************
uint8_t time_cd=250;

uint8_t fun_cd(void)
{
	if(time_cd==0x00)
	{
		time_cd=250;
		return 1;
	}
	else --time_cd;
	if((key_short)||(key_cont)||(key_long)) time_cd=250;
	return 0;
}

//*********************************************
//
//             倒数子函数
//
//*********************************************
//uint8_t time_cd = 250;

uint8_t countDown(uint16_t value0seOrGas,uint8_t zeroOrGas)//zeroOrGas 0 zero 1 gas
{
    static uint8_t timeCount20ms=20;
    time_cd=250;
    if(bf)
    {
        timeCount20ms--;
        if(timeCount20ms<4)
        {
            value.valueSum+=value0seOrGas;
        }
    }
	if(timeCount20ms==00)
	{
		timeCount20ms=20;
		return 1;
	}
	if(controlS&0x01)
	{
		if(zeroOrGas)
		{
		    number_turn(value0seOrGas,pointS);
		}
		else
		{
		    number_turn(value0seOrGas,0);
		}
	}
	else
	{
	    number_turn(timeCount20ms,0);
	}
	if(bf)
	{
	    if(zeroOrGas)
	    {
	        //gas_on;
	    }
	    else
	    {
	        //zero_on;
	    }
	}
	bf^=0x01;
	return 0;
}

/**********************************************************************

                        yes?no?

**********************************************************************/
uint8_t yesNO=0x01;

uint8_t fun_yes(uint8_t symbolX)
{
    if((key_short)||(key_long))
	{
	    cs_add=disNew-1;
	    bf=0x01;
	}
	/*else
	{
	    bf^=0x01;
	}*/
	if (key_short==0b00000100)//||(key_cont==0b00000100))
	{
	    yesNO = 0x01;
	}
	if (key_short==0b00000010)//||(key_cont==0b00000010))
	{
	    yesNO = 0x00;
	}
    if(displayImmediately)
	{
		if(bf)
		{
		    if (yesNO)
		    {
		        symbol_turn(symbolYes);
		    }
		    else
		    {
		        symbol_turn(symbolNo);
		    }
		}
		if (symbolX == symbol0se)
		{
		    //zero_on;
		}
		else if (symbolX == symbolGas)
		{
		    //gas_on;
		}
		bf^=0x01;
	}
	if (key_short == 0x01)
	{
	    if (yesNO == 0x01)
	    {
	        return 0x01;
	    }
	    else
	    {
	        return 0x02;
	    }
	    //yesNO = 0x01;
	}
	return 0x00;
}



//***************************************************
//
//                  key_long
//
//***************************************************

unsigned char m1=0x00;
unsigned char m2=0x00;
unsigned char m3=0x00;

uint8_t fun_key_long(uint8_t m,uint8_t mn)
{
	if((m>=5)&&(m<55))
	{
	    if(mn==1)
	    {
	        //zero_on;
	    }
	    else if(mn==2)
	    {
	        //gas_on;
	    }
	    //batC_on;
	    if(m==0) cs_add=disNew-1;
	}
	/*else if((m>=55)&&(m<105))
	{
	    bat_on;
	}*/
	else if((m>=105)&&(m<=155))
	{
	    if(mn==1)
	    {
	        //zero_on;
	    }
	    else if(mn==2)
	    {
	        //gas_on;
	    }
	    //batB_on;
	    if(m==155)
	    {
	        return 1;
	    }
	}
	/*else if((m>=155)&&(m<205))
	{
	    bat_on;
	}*/
	/*else if((m>=205)&&(m<=255))
	{
	    if(mn==1)
	    {
	        zero_on;
	    }
	    else if(mn==2)
	    {
	        gas_on;
	    }
	    //batA_on;
	    if(m==255)
	    {
	        return 1;
	    }
	}*/
	return 0;
}


//***************************************************
//
//                 gas
//
//***************************************************
void gas(void)
{
	uint16_t g=0x0000;
//	unsigned int g_o2=0x00;
	switch(value.dir)
	{
		case 0:
			if(value.g_wADCValue>value.valueZero)
			{
			    g=value.g_wADCValue-value.valueZero;
			}
			else
			{
			    g=0;
			}
//			g=value.g_wADCValue-value.value_zero;
			if(g>threshold)
			{
			    value.valueGas=(unsigned int)(((unsigned long)g*(unsigned long)value.valueMultiple)>>7);
			}
			else value.valueGas=0x0000;
		break;
		case 1:
			if(value.valueZero>value.g_wADCValue)
			{
			    g=value.valueZero-value.g_wADCValue;
			}
			else
			{
			    g=0;
			}
//			g=value.value_zero-value.g_wADCValue;
			if(g>threshold)
			{
			    value.valueGas=(unsigned int)(((unsigned long)g*(unsigned long)value.valueMultiple)>>7);
			}
			else value.valueGas=0x0000;
		break;
		case 2://O2的multiple 表示浓度0时候的AD数值
			if(value.g_wADCValue<=value.valueMultiple+threshold)
			{
			    value.valueGas=0;
			}
			else if(((value.g_wADCValue+threshold)>value.valueZero)
                    &&(value.g_wADCValue<(value.valueZero+threshold)))
            {
                value.valueGas=209;
            }
			else
			{
			    value.valueGas=(uint16_t)(((uint32_t)(value.g_wADCValue-value.valueMultiple)*(uint32_t)(209))
                                /(uint32_t)(value.valueZero-value.valueMultiple));
			}
/*			if((value.g_wADCValue+threshold)<value.valueZero)
			{
				g_o2=(unsigned int)(((unsigned long)(value.valueZero-value.g_wADCValue)*(unsigned long)value.valueMultiple)>>7);
				if(g_o2>=gas0) g_o2=gas0;
				value.valueGas=gas0-g_o2;
			}
			else if(value.g_wADCValue>(threshold+value.valueZero))
			{
			    value.valueGas=gas0+(unsigned int)(((unsigned long)(value.g_wADCValue-value.valueZero)*(unsigned long)value.valueMultiple)>>7);
			}
			else value.valueGas=gas0; //209*/
		break;
	}
}


//*********************************************
//
//              high-low
//
//*********************************************
uint8_t lowHigh(uint16_t (* value_p),uint8_t low_high) // low_high 0 low 1high
{
	if((key_short)||(key_long))
	{
	    cs_add=disNew-1;
	    bf=0x01;
	}
	else
	{
	    bf^=0x01;
	}
	if((key_short==0b00000100)||(key_cont==0b00000100))
	{
		if(low_high)
		{
			if(value.dir==0x02)
			{
				if((* value_p)<value.valueLow) (* value_p)++;
			}
			else if((value.dir==0x00)||(value.dir==0x01))
			{
				if((* value_p)<a0) (* value_p)++;
			}
		}
		else
		{
			if(value.dir==0x02)
			{
				if((* value_p)<a0) (* value_p)++;
			}
			else if((value.dir==0x00)||(value.dir==0x01))
			{
				if((* value_p)<value.valueHigh) (* value_p)++;
			}
		}
	}
	if((key_short==0b00000010)||(key_cont==0b00000010))
	{
		if(low_high)
		{
			if(value.dir==0x02)
			{
				if((* value_p)>0) (* value_p)--;
			}
			else if((value.dir==0x00)||(value.dir==0x01))
			{
				if((* value_p)>value.valueLow) (* value_p)--;
			}
		}
		else
		{
			if(value.dir==0x02)
			{
				if((* value_p)>value.valueHigh) (* value_p)--;
			}
			else if((value.dir==0x00)||(value.dir==0x01))
			{
				if((* value_p)>0) (* value_p)--;
			}
		}
	}
	if(key_short==0b00000001)
	{
		cli();
		eeprom_write_word((void *)(0x20+8*low_high), (* value_p));
		sei();
		if(low_high)
		{
			if(ee_w0&0x02)
			{
				ee_w0&=0b11111101;
				cli();
				eeprom_write_byte((void *)(0x3e), ee_w0);
				sei();
			}
		}
		else
		{
			if(ee_w0&0x01)
			{
				ee_w0&=0b11111110;
				cli();
				eeprom_write_byte((void *)(0x3e), ee_w0);
				sei();
			}
		}
		return 1;
	}
	if(bf)
	{
	    number_turn(*value_p,pointS);
	}
	if(low_high)
	{
	    //high_on;
	}
	else
	{
	    //low_on;
	}
	//ppm_lel_vol_on;
	return 0;
}

//**********************************************
//
//                current
//
//**********************************************
uint8_t fun_ma(uint16_t (* value_ma),uint8_t f_ma)
{
	uint8_t i_fma=0x00;
	if((key_short)||(key_long))
	{
	    cs_add=disNew-1;
	    bf=0x01;
	}
	else
	{
	    bf^=0x01;
	}
	if((key_short==0b00000100)||(key_cont==0b00000100)) (* value_ma)++;
	if((key_short==0b00000010)||(key_cont==0b00000010)) (* value_ma)--;
	switch(f_ma)
	{
		case 0x00:
			if((* value_ma)==0) (* value_ma)=1;
			if((* value_ma)==0x03ff) (* value_ma)=0x03fe;
		break;
		case 0x01: //4ma
			if((* value_ma)==value.valueCurrent8ma) (* value_ma)=value.valueCurrent8ma-1;
			if((* value_ma)==0) (* value_ma)=1;
   //         time_cd=250;
    //        cli();
     //       OCR1A=10*(* value_ma);
     //       sei();

		break;
		case 0x02: //8
			if((* value_ma)==value.valueCurrent12ma) (* value_ma)=value.valueCurrent12ma-1;
			if((* value_ma)==value.valueCurrent4ma) (* value_ma)=value.valueCurrent4ma+1;
		break;
		case 0x03: //12
			if((* value_ma)==value.valueCurrent16ma) (* value_ma)=value.valueCurrent16ma-1;
			if((* value_ma)==value.valueCurrent8ma) (* value_ma)=value.valueCurrent8ma+1;
		break;
		case 0x04: //16
			if((* value_ma)==value.valueCurrent20ma) (* value_ma)=value.valueCurrent20ma-1;
			if((* value_ma)==value.valueCurrent12ma) (* value_ma)=value.valueCurrent12ma+1;
		break;
		case 0x05: //20
			if((* value_ma)==0x03ff) (* value_ma)=0x03fe;
			if((* value_ma)==value.valueCurrent16ma) (* value_ma)=value.valueCurrent16ma+1;
		break;
	}
	if(key_short==0b00000001)
	{
		cli();
		eeprom_write_word((void *)(0x32+2*f_ma), (* value_ma));
		sei();
		for(i_fma=0;i_fma<6;i_fma++)
		{
			if(i_fma==f_ma)
			{
				if(ee_w0&(1<<(i_fma+2)))
				{
					ee_w0&=(~(1<<(i_fma+2)));
					cli();
					eeprom_write_byte((void *)(0x3e), ee_w0);
					sei();
				}
			}
		}
		return 1;
	}
    if(bf)
	{
	    number_turn(*value_ma,0);
	}
	return 0;
}
/*
void current(void)
{
	uint16_t ocr_hl=0x0000;
	if(value.valueGas>a0) value.valueGas=a0;
	if((value.valueGas>=0)&&(value.valueGas<a_8))
	{
		ocr_hl=(unsigned int)(((unsigned long)value.valueGas
                *(unsigned long)(value.valueCurrent8ma-value.valueCurrent4ma))/a_8)+value.valueCurrent4ma;
	}
	else if((value.valueGas>=a_8)&&(value.valueGas<a_12))
	{
		ocr_hl=(unsigned int)(((unsigned long)(value.valueGas-a_8)
                *(unsigned long)(value.valueCurrent12ma-value.valueCurrent8ma))/a_8)+value.valueCurrent8ma;
	}
	else if((value.valueGas>=a_12)&&(value.valueGas<a_16))
	{
		ocr_hl=(unsigned int)(((unsigned long)(value.valueGas-a_12)
                *(unsigned long)(value.valueCurrent16ma-value.valueCurrent12ma))/a_8)+value.valueCurrent12ma;
	}
	else if((value.valueGas>=a_16)&&(value.valueGas<=a0))
	{
		ocr_hl=(unsigned int)(((unsigned long)(value.valueGas-a_16)
                *(unsigned long)(value.valueCurrent20ma-value.valueCurrent16ma))/a_8)+value.valueCurrent16ma;
	}
	cli();
	OCR1A=ocr_hl;
	sei();
}*/











